<?php

include('XMLImporter.php');

$dbConfig = Array
(
    'dbtype' => "MYSQL",
    'host' => "localhost",
    'database' => "test",
    'user' => "root",
    'password' => "root"
);

$fieldsMap = Array
(
    'bus_id' => Array(Type::TYPE => ValueType::VALUE,
        "value" => "2"),

    'sales_title' => Array(Type::TYPE => ValueType::TAG,
        "value" => "title"),

    'sales_desc' => Array(Type::TYPE => ValueType::TAG,
        "value" => "highlightsHtml"),

    'sale_starts' => Array(Type::TYPE => ValueType::TAG,
        "value" => "startAt",
        "default" => date("Y.m.d"),
        "action" => "processDate"),

    'sale_ends' => Array(Type::TYPE => ValueType::TAG,
        "value" => "endAt",
        "default" => date("Y.m.d", strtotime("+1 month")),
        "action" => "processDate"),

    'not_sure' => Array(Type::TYPE => ValueType::VALUE,
        "value" => "0",
        "dependsOn" => "sale_ends"),

    'google_map' => Array(Type::TYPE => ValueType::VALUE,
        "value" => "online"),

    'website' => Array(Type::TYPE => ValueType::VALUE,
        "value" => "www.groupon.com"),

    'photo' => Array(Type::TYPE => ValueType::VALUE,
        "value" => "groupon.jpg"),

    'thumbnail' => Array(Type::TYPE => ValueType::VALUE,
        "value" => "groupon.jpg"),

    'affiliate_link' => Array(Type::TYPE => ValueType::TAG,
        "value" => "dealUrl",
        "action" => "getAffilateLink"),

    'affiliate_product_id' => Array(Type::TYPE => ValueType::TAG,
        "value" => "id",
        "key" => "true"),

    'active' => Array(Type::TYPE => ValueType::VALUE,
        "value" => "1"),

    'is_featured' => Array(Type::TYPE => ValueType::VALUE,
        "value" => "0"),

    'price' => Array(Type::TYPE => ValueType::TAG,
        "value" => "options->option->price->amount"),
);

$imagesFieldsMap = Array(
    'file_url' => Array(Type::TYPE => ValueType::TAG,
        "value" => "largeImageUrl"),

    'sales_id' => Array(Type::TYPE => ValueType::VALUE,
        "value" => "This id will be set in action method",
        "action" => "getSaleIdFromItem"),

    'image_path' => Array(Type::TYPE => ValueType::TAG,
        "value" => "largeImageUrl",
        "action" => 'downloadImage'),

    'extension' => Array(Type::TYPE => ValueType::TAG,
        "value" => "largeImageUrl",
        "action" => 'getFileExtension')
);

$categoryFieldsMap = Array(
    'sales_id' => Array(Type::TYPE => ValueType::VALUE,
        "value" => "This id will be set in action method",
        "action" => "getSaleIdFromItem"),

    'cat_id' => Array(Type::TYPE => ValueType::TAG_INDEX,
        "value" => "dealTypes->dealType->id",
        "index" => "0",
        "action" => "getCategoryByTitle"));

$secondaryCategoryFieldsMap = Array(
    'sales_id' => Array(Type::TYPE => ValueType::VALUE,
        "value" => "This id will be set in action method",
        "action" => "getSaleIdFromItem"),

    'cat_id' => Array(Type::TYPE => ValueType::TAG_INDEX,
        "value" => "dealTypes->dealType->id",
        "index" => "1",
        "action" => "getGrouponSubcategoryByTitle"));

$thirdCategoryFieldsMap = Array(
    'sales_id' => Array(Type::TYPE => ValueType::VALUE,
        "value" => "This id will be set in action method",
        "action" => "getSaleIdFromItem"),

    'cat_id' => Array(Type::TYPE => ValueType::TAG_INDEX,
        "value" => "dealTypes->dealType->id",
        "index" => "2",
        "action" => "getGrouponSubcategoryByTitle"));

$xmlFilePath = "./Misc/deals2_small.xml";
$imagesFolder = "./images/";

$importer = new XMLImporter($dbConfig, $fieldsMap, $xmlFilePath, $imagesFolder, "deals->deal");
list($uniqueItems, $firstItemId, $lastItemId) = $importer->parseFile(Constants::TBL_SALES, Constants::TBL_SALES_ID);

$importer->mapAdditionalTable($importer->copyArray($uniqueItems), $firstItemId, $lastItemId, $imagesFieldsMap, Constants::TB_SALES_IMAGE, Constants::TBL_SALES_ID);
$importer->mapAdditionalTable($importer->copyArray($uniqueItems), $firstItemId, $lastItemId, $categoryFieldsMap, Constants::TB_SALES_CATEGORY, Constants::TBL_SALES_ID);
$importer->mapAdditionalTable($importer->copyArray($uniqueItems), $firstItemId, $lastItemId, $secondaryCategoryFieldsMap, Constants::TB_SALES_CATEGORY, Constants::TBL_SALES_ID);
$importer->mapAdditionalTable($importer->copyArray($uniqueItems), $firstItemId, $lastItemId, $thirdCategoryFieldsMap, Constants::TB_SALES_CATEGORY, Constants::TBL_SALES_ID);
?>